package com.cf_houduan.cf_houduan.service;

import com.cf_houduan.cf_houduan.constant.BusinessException;
import com.cf_houduan.cf_houduan.dao.BaseDao;
import com.cf_houduan.cf_houduan.enums.MatnFlagEnum;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;



import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CommonService<P> implements BaseService<P> {

    @Autowired
    private BaseDao baseDao;

    @Override
    public Integer count(P selectPo) {
        return null;
    }

    @Override
    public PageInfo<P> selectList(P selectPo, int pageIndex, int pageSize) {
        return comSelectList(selectPo,pageIndex,pageSize);
    }

    @Override
    public P selectOne(P selectPo) {
        return comSelectOne(selectPo);
    }

    @Override
    public void addPo(P selectPo) {
        //参数校验
        checkParam(selectPo,MatnFlagEnum.MATN_FLAG_ADD.getCode());
        //新增
        comInsert(selectPo);
    }

    @Override
    public void deletePo(P selectPo) {
        //参数校验
        checkParam(selectPo,MatnFlagEnum.MATN_FLAG_DEL.getCode());
        //删除
        comDelete(selectPo);
    }

    /**
     *
     * @param updatePo 修改实体类
     * @param selectPo 条件实体类
     */
    @Override
    public void updatePo(P updatePo, P selectPo) {
        //参数校验
        checkParam(selectPo,MatnFlagEnum.MATN_FLAG_UPT.getCode());
        //修改
        comUpdate(updatePo,selectPo);
    }

    //公共参数校验
    public void checkParam(P selectPo,String type){
        if (type.equals(MatnFlagEnum.MATN_FLAG_ADD.getCode())){
            P p= comSelectOne(selectPo);
            if (p != null){
                throw new BusinessException("该新增数据已经存在!");
            }
        } else if (type.equals(MatnFlagEnum.MATN_FLAG_UPT.getCode())){
            if(selectPo == null){
                throw new BusinessException("需修改的数据不存在!");
            }
        } else if (type.equals(MatnFlagEnum.MATN_FLAG_DEL.getCode())){
            if(selectPo == null){
                throw new BusinessException("需删除的数据不存在!");
            }
        }
    }

    //新增
    public final void comInsert(P selectPo){
        baseDao.addPo(selectPo);
    }

    //修改
    public final void comUpdate(P updatePo,P selectPo){
        Map<String,Object> paramter = new HashMap<>();
        paramter.put("u",updatePo);
        paramter.put("s",selectPo);
        baseDao.updatePo(paramter);
    }

    //删除
    public final void comDelete(P selectPo){
        baseDao.deletePo(selectPo);
    }

    //分页查询
    public final PageInfo<P> comSelectList(P selectPo, int pageIndex, int pageSize){
        PageHelper.startPage(pageIndex,pageSize);
        List list = baseDao.selectList(selectPo);
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }

    //查询数量
    public final Integer comCount(P selectPo){
        return baseDao.count(selectPo);
    }

    //查询单条
    public final P comSelectOne(P selectPo){
        return (P) baseDao.selectOne(selectPo);
    }




}
